package com.example.sps.handler;

import com.example.sps.commons.Constants;
import com.example.sps.commons.ServiceCode;
import com.example.sps.commons.ServiceException;
import com.example.sps.utils.JWTUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ProjectName: Sps
 * @Titile: TokenInterceptor
 * @Author: nency
 * @Description: TODO
 */
@Component
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        return HandlerInterceptor.super.preHandle(request, response, handler);
        String token = request.getHeader(Constants.TOKEN_HEADER);
        if(!StringUtils.hasText(token)){
            throw new ServiceException(ServiceCode.ERROR,"token不能为空");
        }
        JWTUtils.validateToken(token);
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
